package com.dp.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dp.common.annotation.Log;
import com.dp.common.core.controller.BaseController;
import com.dp.common.core.domain.AjaxResult;
import com.dp.common.enums.BusinessType;
import com.dp.system.domain.SysTemperature;
import com.dp.system.service.ISysTemperatureService;
import com.dp.common.utils.poi.ExcelUtil;
import com.dp.common.core.page.TableDataInfo;

/**
 * 温度记录Controller
 *
 * @author ruoyi
 * @date 2025-02-10
 */
@RestController
@RequestMapping("/system/temperature")
public class SysTemperatureController extends BaseController
{
    @Autowired
    private ISysTemperatureService sysTemperatureService;

    /**
     * 获取各个大棚的实时温度数据
     * @return 大棚温度数据列表
     */
    private static final Logger logger = LoggerFactory.getLogger(SysTemperatureController.class);

    @GetMapping("/real-time-with-name")
    public AjaxResult realTimeTemperatureDataWithName() {
        List<SysTemperature> data = sysTemperatureService.selectRealTimeTemperatureDataWithName();
        return AjaxResult.success(data);
    }

    /**
     * 查询温度记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:temperature:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTemperature sysTemperature)
    {
        startPage();
        List<SysTemperature> list = sysTemperatureService.selectSysTemperatureList(sysTemperature);
        return getDataTable(list);
    }

    /**
     * 导出温度记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:temperature:export')")
    @Log(title = "温度记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTemperature sysTemperature)
    {
        List<SysTemperature> list = sysTemperatureService.selectSysTemperatureList(sysTemperature);
        ExcelUtil<SysTemperature> util = new ExcelUtil<SysTemperature>(SysTemperature.class);
        util.exportExcel(response, list, "温度记录数据");
    }

    /**
     * 获取温度记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:temperature:query')")
    @GetMapping(value = "/{temperatureId}")
    public AjaxResult getInfo(@PathVariable("temperatureId") Long temperatureId)
    {
        return success(sysTemperatureService.selectSysTemperatureByTemperatureId(temperatureId));
    }

    /**
     * 新增温度记录
     */
    @PreAuthorize("@ss.hasPermi('system:temperature:add')")
    @Log(title = "温度记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTemperature sysTemperature)
    {
        return toAjax(sysTemperatureService.insertSysTemperature(sysTemperature));
    }

    /**
     * 修改温度记录
     */
    @PreAuthorize("@ss.hasPermi('system:temperature:edit')")
    @Log(title = "温度记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTemperature sysTemperature)
    {
        return toAjax(sysTemperatureService.updateSysTemperature(sysTemperature));
    }

    /**
     * 删除温度记录
     */
    @PreAuthorize("@ss.hasPermi('system:temperature:remove')")
    @Log(title = "温度记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{temperatureIds}")
    public AjaxResult remove(@PathVariable Long[] temperatureIds)
    {
        return toAjax(sysTemperatureService.deleteSysTemperatureByTemperatureIds(temperatureIds));
    }

    /**
     * 获取温度历史数据，用于绘制折线图
     */
    @GetMapping("/historyData")
    public AjaxResult getTemperatureHistoryData() {
        List<SysTemperature> data = sysTemperatureService.getTemperatureHistoryData();
        return success(data);
    }


}